package com.learn.project2.service.admin.footprint;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.project2.bean.MarketCollect;
import com.learn.project2.bean.MarketCollectExample;
import com.learn.project2.bean.MarketFootPrint;
import com.learn.project2.bean.MarketFootPrintExample;
import com.learn.project2.mapper.MarketFootPrintMapper;
import com.learn.project2.model.bo.admin.user.UserListBO;
import com.learn.project2.model.vo.admin.collect.CollectListVO;
import com.learn.project2.model.vo.admin.footprint.FootprintListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author wxy
 * @date 2022-01-08 11:02
 * @version 1.0
 */
@Service
@CacheConfig(cacheNames = "marketFootprint")
public class FootprintServiceImpl implements FootprintService {
    @Autowired
    MarketFootPrintMapper footPrintMapper;
    @Override
    @Cacheable
    public FootprintListVO selectFootprint(Integer userId, Integer goodsId, UserListBO userListBO) {

        Integer limit = userListBO.getLimit();
        Integer page = userListBO.getPage();
        String sort = userListBO.getSort();
        String order = userListBO.getOrder();
        //使用分页查询
        PageHelper.startPage(page,limit);

        MarketFootPrintExample footPrintExample = new MarketFootPrintExample();
        MarketFootPrintExample.Criteria criteria = footPrintExample.createCriteria();
        //userId,goodsId精确查询
        if (userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if (goodsId != null){
            criteria.andGoodsIdEqualTo(goodsId);
        }
        criteria.andDeletedEqualTo(false);
        //给查询条件
        footPrintExample.setOrderByClause(sort+" "+order);
        List<MarketFootPrint> list = footPrintMapper.selectByExample(footPrintExample);

        PageInfo<MarketFootPrint> pageInfo = new PageInfo<>(list);
        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        return FootprintListVO.data(total,pages,limit,page,list);

    }
}
